package com.example.demo.day0305;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.*;

public class TestCheck {
    public static void main(String[] args) throws IOException {
        Calculator c = new Calculator();//获取对象
        Class cls = c.getClass();//获取字节码文件对象
        Method[] methods = cls.getMethods();//获取所有方法

        int number = 0;//初始化一个number作为出现异常的次数
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\demo\\src\\main\\java\\com\\example\\demo\\day0305\\bug.txt"));
        //用缓冲字符流输出bw到bug.txt中
        for (Method method : methods){//遍历Calculator中的所有方法
            if (method.isAnnotationPresent(Check.class)){//判断方法上是否有Check注解
                try {
                    method.invoke(c);
                } catch (Exception e) {
                    e.printStackTrace();
                    number++;
                    bw.write(method.getName() + "方法有异常\n" + "异常的名称" + e.getCause().getClass().getSimpleName() + "\n" + "异常的原因" + e.getCause().getMessage() + "\n");

                }

            }
        }
        bw.write("一共出现了" + number + "次异常" );
        bw.flush();
        bw.close();
    }
}
